package org.sixtynine.stock.dao.impl;

/**
 * @author Thilan
 *
 */

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.sixtynine.stock.dao.IpgReturnDao;
import org.sixtynine.stock.dto.GridPramDTO;
import org.sixtynine.stock.dto.Page;
import org.sixtynine.stock.entity.IpgReturn;
import org.springframework.stereotype.Repository;

@Repository
public class IpgReturnDaoImpl extends AbstractDaoImpl<IpgReturn, Integer>
		implements IpgReturnDao {

	protected IpgReturnDaoImpl() {
		super(IpgReturn.class);
	}

	@Override
	public void saveIpgReturn(IpgReturn ipgReturn) {
		saveOrUpdate(ipgReturn);
	}

	@Override
	public IpgReturn searchIpgReturn(String orderId) {
		if (findByCriteria(Restrictions.eq("orderId", orderId)).size() > 0) {
			return findByCriteria(Restrictions.eq("orderId", orderId)).get(0);
		} else {
			return null;
		}

	}

	@Override
	public List<IpgReturn> searchIpgReturn(Date fromDate, Date toDate,
			String orderBy) {
		Criteria criteria = getCurrentSession().createCriteria(IpgReturn.class);

		if (fromDate != null) {
			criteria.add(Restrictions.ge("addedDate", fromDate));
		}

		if (toDate != null) {
			criteria.add(Restrictions.le("addedDate", toDate));
		}

		return findByCriteria(criteria);
	}

	@Override
	public Page<IpgReturn> searchIpgReturn(Date fromDate, Date toDate,
			String orderBy, GridPramDTO gridPramDTO) {
		Criteria criteria = getCurrentSession().createCriteria(IpgReturn.class);

		criteria.addOrder(Order.desc(getRecordOrder(orderBy)));

		if (fromDate != null) {
			criteria.add(Restrictions.ge("addedDate", fromDate));
		}

		if (toDate != null) {
			criteria.add(Restrictions.le("addedDate", toDate));
		}

		return findByCriteria(criteria, gridPramDTO);
	}

}
